import { Ref, ref } from 'vue';

import { deserialize } from '@/plugins/reflect-metadata';

import { getAuthList } from '../api';
import { AuthMeta } from '../models/AuthItem';
import { AuthList } from '../models/AuthList';

const useReportList = () => {
  const state: Ref<{
    data: AuthMeta[];
    loading: boolean;
  }> = ref({
    data: [],
    loading: true,
  });
  const handleFetch = async () => {
    try {
      state.value.loading = true;
      const { data } = await getAuthList();
      const resultData = deserialize(data.data, AuthList);
      state.value.data = resultData.items;
    } finally {
      state.value.loading = false;
    }
  };

  return {
    state,
    handleFetch,
  };
};

export default useReportList;
